package com.bkgy.modularbx.processrule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.config.BusinessException;
import com.bkgy.modularbx.processrule.domain.ProcessRuleLibrary;
import com.bkgy.modularbx.processrule.dto.ProcessRuleLibraryDto;
import com.bkgy.modularbx.processrule.mapper.ProcessRuleLibraryMapper;
import com.bkgy.modularbx.processrule.service.ProcessRuleLibraryService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author dell
 * @description 针对表【TB_DSJ_PROCESS_RULE_LIBRARY(工艺规则库表)】的数据库操作Service实现
 * @createDate 2025-04-21 10:35:49
 */
@Service
@Transactional
public class ProcessRuleLibraryServiceImpl extends ServiceImpl<ProcessRuleLibraryMapper, ProcessRuleLibrary>
        implements ProcessRuleLibraryService {

    @Override
    public ResultData<Void> addProcessRule(ProcessRuleLibrary processRuleLibrary) {
        ProcessRuleLibrary processRule = getById(processRuleLibrary.getProcessRuleVariable());
        if (processRule == null) {
            processRuleLibrary.setToc(new Date());
            processRuleLibrary.setTom(new Date());
            save(processRuleLibrary);
        } else {
            ResultData.ResultDataFaultSelf("工艺规则库变量名重复", null);
        }
        return ResultData.ResultDataSuccessSelf("添加成功", null);
    }

    @Override
    public ResultData<Page<ProcessRuleLibrary>> pageProcessRule(QueryDTO<ProcessRuleLibraryDto> queryDTO) {
        ProcessRuleLibraryDto dto = queryDTO.getDto();
        LambdaQueryWrapper<ProcessRuleLibrary> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        if (dto != null) {
            wrapper.like(StringUtils.hasText(dto.getProcessRuleVariable()), ProcessRuleLibrary::getProcessRuleVariable, dto.getProcessRuleVariable());
            wrapper.like(StringUtils.hasText(dto.getVariableDescription()), ProcessRuleLibrary::getVariableDescription, dto.getVariableDescription());
        }

        Page<ProcessRuleLibrary> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        Page<ProcessRuleLibrary> result = page(page, wrapper);
        return ResultData.ResultDataSuccess(result, (int) result.getTotal());
    }

    @Override
    public ResultData<Void> deleteProcessRule(String processRuleVariable) {
        ProcessRuleLibrary processRule = getById(processRuleVariable);
        if (processRule == null) {
            throw new BusinessException("要删除的工艺规则不存在");
        }
        removeById(processRuleVariable);
        return ResultData.ResultDataSuccessSelf("删除成功", null);
    }

    @Override
    public ResultData<Void> updateProcessRule(ProcessRuleLibrary processRuleLibrary) {
        ProcessRuleLibrary existProcessRule = getById(processRuleLibrary.getProcessRuleVariable());
        if (existProcessRule == null) {
            throw new BusinessException("要修改的工艺规则不存在");
        }
        processRuleLibrary.setToc(new Date());
        updateById(processRuleLibrary);
        return ResultData.ResultDataSuccessSelf("修改成功", null);
    }

    @Override
    public ResultData<ProcessRuleLibrary> findById(String processRuleVariable) {
        ProcessRuleLibrary processRule = getById(processRuleVariable);
        if (processRule == null) {
            throw new BusinessException("工艺规则不存在");
        }
        return ResultData.ResultDataSuccess(processRule, 1);
    }
}




